﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace Web7.Core.Security
{
    public class PermissionProviderService : IPermission
    {
        /// <summary>
        /// 猥琐的做法 属性注入就是不行
        /// </summary>
        public List<IPermissionProvider> PermissionProviders
        {
            get
            {
                List<IPermissionProvider> list;
                IDependencyResolver dependencyResolver = DependencyResolver.Current;
                list = dependencyResolver.GetServices<IPermissionProvider>().ToList();
                list.Add(dependencyResolver.GetService<IPermissionProvider>());
                return list;
            }
        }

        public Permission GetPermissionById(Guid permissionId)
        {
            return PermissionProviders.SelectMany(permissionProvider => permissionProvider.GetPermissions()).FirstOrDefault(permission => permission.Id == permissionId);
        }

        public Permission GetPermissionByName(string Name)
        {
            return PermissionProviders.SelectMany(permissionProvider => permissionProvider.GetPermissions()).FirstOrDefault(permission => permission.Name == Name);
        }

        public IList<Permission> GetAllPermissions()
        {
            return PermissionProviders.SelectMany(permissionProvider => permissionProvider.GetPermissions()).ToList();
        }

        public void InsertPermission(Permission permission)
        {
            throw new NotImplementedException();
        }

        public void DeletePermission(Permission permission)
        {
            throw new NotImplementedException();
        }

        public void UpdatePermission(Permission permission)
        {
            throw new NotImplementedException();
        }

        public void InstallPermissions(IPermissionProvider permissionProvider)
        {
            throw new NotImplementedException();
        }

        public void UninstallPermissions(IPermissionProvider permissionProvider)
        {
            throw new NotImplementedException();
        }
    }
}
